for
loops.from | toThe pipe operator is an infix shorthand for calling the
pipe
generic function, which you can specialize in a scripted class. Tables 5-1 and 4-2 describe how pipe
is implemented by certain classes in the core classes.The following examples demonstrate the use of pipes:
1 to 5 | LinkedList
#(1, 2, 3, 4, 5) as LinkedList
#(3,4,5,6) | #(1,2)
#(1, 2, 3, 4, 5, 6)
function double n -> 2 * n
1 to 10 | double
#(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
#(1, 2, 3, 4) | (n -> 2 * n)
#(2, 4, 6, 8)
You can also "cascade" pipe expressions, that is, string two or more pipe expressions together.
function squares n -> n * n
1 to 5 | double | squares
A useful trick is to pipe a long collection to the#(4, 16, 36, 64, 100)
print
function. The collection would otherwise be displayed in truncated form. The following example collects a list of generic functions that the Rect
class implements. Only the first few values are reproduced here.getAllGenerics Rect | print
init()
localEqual()
prin()
morph()
copy()
. . .
As an intermediate step, the collection could be piped to SortedArray
, so that it is alphabetized.
getAllGenerics Rect | SortedArray | print
bboxGetter()
copy()
drawSelf()
finalize()
heightGetter()
heightSetter()
. . .
This script creates a list of classes of objects that implement the generic function pipe
. Note the use of an anonymous function. The anonymous function is especially useful here as a "package" around canObjectDo
, a generic function that requires two arguments. Since pipe
works only with functions requiring one argument, the anonymous function solves the problem. It takes the one argument supplied by pipe
, passes it to canObjectDo
, and supplies pipe
as the second argument to canObjectDo
. The result of the anonymous function is then passed on to the next pipe.
getSubs RootObject | (y -> if canObjectDo y pipe then y else empty) \
| SortedArray | print
AccessoryContainer
ActionList
ActuatorController
ApplyTree
Array
ArrayList
Btree
ByteString
. . .
This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.